package com.jiandian.service.impl;

import com.jiandian.consts.DictConsts;
import com.jiandian.dao.BannerMapper;
import com.jiandian.entity.Banner;
import com.jiandian.entity.Dict;
import com.jiandian.page.PageInfo;
import com.jiandian.page.WhereMapUtil;
import com.jiandian.service.BannerService;
import com.jiandian.service.DictService;
import com.jiandian.service.OssService;
import com.jiandian.util.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * 轮播图片
 *
 * @author Houqi Fu
 * @date 2019/01/24
 */
@Service
public class BannerServiceImpl implements BannerService {

    private static final Logger logger = LoggerFactory.getLogger(BannerServiceImpl.class);

    @Autowired
    private BannerMapper mapper;

    @Autowired
    private OssService ossService;

    @Autowired
    private DictService dictService;

    @Override
    public Banner add(Banner record) {

        int result = mapper.insert(record);
        if (result > 0) {
            return record;
        }
        return null;
    }

    @Override
    public Banner update(Banner record) {
        Integer id = record.getId();
        if (id == null) {
            return null;
        }
        Banner dbBanner = mapper.selectByPrimaryKey(id);
        if (dbBanner == null) {
            return null;
        }
        BeanUtils.copyProperties(record, dbBanner);
        dbBanner.setGmtModified(new Timestamp(System.currentTimeMillis()));
        int result = mapper.updateByPrimaryKey(dbBanner);
        if (result > 0) {
            return dbBanner;
        }
        return null;
    }

    @Override
    public Banner findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Banner> findAll(PageInfo pageInfo) {
        return mapper.selectAll(pageInfo);
    }

    @Override
    public int selectCount(PageInfo pageInfo) {
        return mapper.selectCount(pageInfo);
    }

    @Override
    public int batchInsert(List<Banner> list) {

        return mapper.batchInsert(list);
    }

    @Override
    public List<Banner> batchQueryByIds(List<Integer> ids) {
        return mapper.batchQueryByIds(ids);
    }

    @Override
    public int delete(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delete(Banner record) {
        return delete(record.getId());
    }

    @Override
    public int batchDeleteById(List<Integer> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            return mapper.batchDeleteById(ids);
        }
        return 0;
    }

    @Override
    public PageInfo getPage(Banner whereEntity, PageInfo pageInfo) {
        pageInfo.setWhereMap(WhereMapUtil.convertBeanToWhereMap(whereEntity));
        pageInfo.setTotalRecord(selectCount(pageInfo));
        pageInfo.queryPageResult(pageInfo);
        pageInfo.setResultObject(findAll(pageInfo));
        return pageInfo;
    }

    @Override
    public String fetchBannerUrls(String bucketName) {
        return ossService.getBannerUrls(bucketName);
    }

    @Override
    public int updateBanner(String url) {
        // todo 旧代码迁移后删除
        int i = selectCount(null);
        if (i == 0) {
            Banner banner = initBasicValue();
            banner.setUrl(url);
            add(banner);

        }else {
            mapper.updateBanner(url, new Timestamp(System.currentTimeMillis()));
        }

        // Timestamp now = new Timestamp(System.currentTimeMillis());
        Dict dict = dictService.findByKey(DictConsts.BANNER);
        if (dict == null) {
            dict = new Dict();
            dict.setKey(DictConsts.BANNER);
            dict.setValue(url);
            dictService.add(dict);
        }else {
            dict.setKey(DictConsts.BANNER);
            dict.setValue(url);
            dictService.update(dict);
        }
        return 1;
    }

    private Banner initBasicValue() {
        Banner record = new Banner();
        Timestamp now = new Timestamp(System.currentTimeMillis());
        record.setGmtCreate(now);
        record.setGmtModified(now);
        return record;
    }

    @Override
    public Dict getBannerNew(String bucketName) {
        Dict banner = dictService.findByKey(DictConsts.BANNER);
        return banner;
    }
}
